var redis = require("redis")
var robots = require("../conf/robot.json")
var lipstickCfg = require("../servers/game/solitary/cfg/lipstick.json")
var runQuicklyGoldCfg = require("../servers/game/match/cfg/runQuicklyGold.json")
var competitionCfg = require("../servers/game/match/cfg/competition.json")
var hostCfg = require("../conf/host.json")
var handler = module.exports
var RDS_PORT = 6379
var RDS_HOST = hostCfg.mainDB
var RDS_PWD = hostCfg.mainPassword
var RDS_OPTS = {auth_pass : RDS_PWD}
var ROOM_ALL_AMOUNT = 20000			   //总房间数量
var ROOM_BEGIN_INDEX = 200800   	   //起始房间ID
handler.start = function(dbService) {
	var db = redis.createClient(RDS_PORT,RDS_HOST,RDS_OPTS)
	db.on("ready",function(res) {
		db.select("14",function(err) {
			dbService.db = db
			//数据库初始配置
			//检查uid
			dbService.db.get("nn:acc:lastid",function(err,data) {
				if(data === null){
			        console.log("\033[33m[INFO] DataBase check - nn:acc:lastid\033[0m");
			        db.set("nn:acc:lastid",10000);
	    		}
			})
			//检查公告
			dbService.db.get("gameServer:notifys",function(err,data) {
				if(data === null){
			        console.log("\033[33m[INFO] DataBase check - gameServer:notifys\033[0m");
			        var notify = {"1" : {"name" : "","content" : "欢迎来到我爱跑得快，祝您游戏愉快"}}
			        db.set("gameServer:notifys",JSON.stringify(notify));
	    		}
			})
			//答题总奖金
			dbService.db.get("game:award:answer:allBonus",function(err,data) {
				if(data === null){
			        console.log("\033[33m[INFO] DataBase check - game:award:answer:allBonus\033[0m");
			        db.set("game:award:answer:allBonus",100000);
	    		}
			})
			//跑得快闯关总奖金
			dbService.db.get("game:award:checkpoint:allBonus",function(err,data) {
				if(data === null){
			        console.log("\033[33m[INFO] DataBase check - game:award:checkpoint:allBonus\033[0m");
			        db.set("game:award:checkpoint:allBonus",2000);
	    		}
			})
			//跑得快闯关奖金类型
			dbService.db.get("game:award:checkpoint:awardType",function(err,data) {
				if(data === null){
			        console.log("\033[33m[INFO] DataBase check - game:award:checkpoint:awardType\033[0m");
			        db.set("game:award:checkpoint:awardType",103);
	    		}
			})
			//闯关关卡奖励
			dbService.db.exists("game:checkpoint:awardInfo",function(err,data) {
				if(err || !data){
					var info = {
						"1" : {
							"itemId" : 101,
							"minValue" : 5,
							"maxValue" : 10
						},
						"2" : {
							"itemId" : 101,
							"minValue" : 6,
							"maxValue" : 12
						},
						"3" : {
							"itemId" : 103,
							"minValue" : 0.1,
							"maxValue" : 0.5
						},
						"4" : {
							"itemId" : 101,
							"minValue" : 7,
							"maxValue" : 14
						},
						"5" : {
							"itemId" : 103,
							"minValue" : 0.2,
							"maxValue" : 0.6
						},
						"6" : {
							"itemId" : 101,
							"minValue" : 10,
							"maxValue" : 20
						},
						"7" : {
							"itemId" : 103,
							"minValue" : 0.3,
							"maxValue" : 0.7
						}
					}
					for(var i in info){
						info[i] = JSON.stringify(info[i])
					}
					dbService.db.hmset("game:checkpoint:awardInfo",info)
				}
			})
			//抽奖次数表
			dbService.db.hexists("gameServer:lotteryCountList","flag",function(err,data) {
				if(err || !data){
					dbService.db.hset("gameServer:lotteryCountList","flag",true)
	    		}
			})
			//获奖列表   gameServer:lotteryAwardList
			//抽奖记录   gameServer:lotteryRecordList
			//重要物品记录表 gameServer:importanceAwardList
			//抽奖配置表
			dbService.db.exists("gameServer:lotteryTable",function(err,data) {
				if(err || !data){
					var info = {
						"0" : JSON.stringify({
							"itemId" : 103,
							"itemName" : "红包",
							"title" : "8元红包",
							"count" : 8,
							"weight" : 0,
							"importance" : true
						}),
						"1" : JSON.stringify({
							"itemId" : 2001,
							"itemName" : "话费券",
							"title" : "一元话费券",
							"count" : 1,
							"weight" : 100
						}),
						"2" : JSON.stringify({
							"itemId" : 1003,
							"itemName" : "参赛券",
							"title" : "参赛券X1",
							"count" : 1,
							"weight" : 300
						}),
						"3" : JSON.stringify({
							"itemId" : 102,
							"itemName" : "金币",
							"title" : "2000金币",
							"count" : 2000,
							"weight" : 2000,
						}),
						"4" : JSON.stringify({
							"itemId" : 103,
							"itemName" : "红包",
							"title" : "5元红包",
							"count" : 5,
							"weight" : 0,
							"importance" : true
						}),
						"5" : JSON.stringify({
							"itemId" : 1003,
							"itemName" : "参赛券",
							"title" : "参赛券X2",
							"count" : 2,
							"weight" : 100
						}),
						"6" : JSON.stringify({
							"itemId" : 102,
							"itemName" : "金币",
							"title" : "800金币",
							"count" : 800,
							"weight" : 2000
						}),
						"7" : JSON.stringify({
							"itemId" : 2010,
							"itemName" : "百元京东E卡",
							"title" : "百元京东E卡",
							"count" : 1,
							"weight" : 0,
							"importance" : true
						})
					}
					dbService.db.hmset("gameServer:lotteryTable",info)
	    		}
			})
			//抽奖库存表
			dbService.db.exists("gameServer:lotteryInventory",function(err,data) {
				if(err || !data){
					var info = {
						"1001" : 100,
						"1002" : 100,
						"1003" : 100,
						"1004" : 100
					}
					dbService.db.hmset("gameServer:lotteryInventory",info)
				}
	    	})
	    	//连续登陆奖励配置表
	    	dbService.db.exists("gameServer:successionLoginAward",function(err,data){
				if(err || !data){
					var info = {
						"0" : JSON.stringify({
							"itemId" : 102,
							"count" : 0
						}),
						"1" : JSON.stringify({
							"itemId" : 102,
							"count" : 0
						}),
						"2" : JSON.stringify({
							"itemId" : 102,
							"count" : 0
						}),
					}
					dbService.db.hmset("gameServer:successionLoginAward",info)
				}
	    	})
			//闯关机器人配置
			dbService.db.exists("game:hunanRunQuickly:checkpointRobots",function(err,data) {
				if(err || !data){
					var info = {
						"1" : 12000,
						"2" : 6400,
						"3" : 3200,
						"4" : 1600,
						"5" : 800,
						"6" : 400,
						"7" : 200,
						"100" : 100
					}
					dbService.db.hmset("game:hunanRunQuickly:checkpointRobots",info)
				}
	    	})
			//闯关机器人默认配置
			dbService.db.exists("game:hunanRunQuickly:checkpointDefault",function(err,data) {
				if(err || !data){
					var info = {
						"1" : 12000,
						"2" : 6400,
						"3" : 3200,
						"4" : 1600,
						"5" : 800,
						"6" : 400,
						"7" : 200,
						"100" : 100
					}
					dbService.db.hmset("game:hunanRunQuickly:checkpointDefault",info)
				}
	    	})
	    	//闯关机器人排行榜
			dbService.db.exists("game:hunanRunQuickly:checkpointRobotsInfo",function(err,data) {
				if(err || !data){
					//机器人信息更新
					var info = {}
					for(var i = 0;i < 20;i++){
						var uid = Math.floor(Math.random() * 1000)
						var tmpObj = {
							uid : uid,
							score : Math.floor(Math.random() * 100) + 100,
							head : robots[uid].head,
							nickname : robots[uid].nickname
						}
						info[i] = JSON.stringify(tmpObj)
					}
					dbService.db.hmset("game:hunanRunQuickly:checkpointRobotsInfo",info)
				}
			})
			//红包任务
			dbService.db.exists("game:redTask:awardInfo",function(err,data) {
				if(err || !data){
					var info = {
						min : 0.2,
						max : 1,
						itemId : 103
					}
					dbService.db.hmset("game:redTask:awardInfo",info)
				}
			})
			//金币场库存
			dbService.db.exists("game:gold:inventory",function(err,data) {
				if(err || !data){
					var info = {}
					for(var i in runQuicklyGoldCfg){
						info[i] = 10000
					}
					console.log(runQuicklyGoldCfg,info)
					dbService.db.hmset("game:gold:inventory",info)
				}
			})
			//比赛场库存
			dbService.db.exists("game:competition:inventory",function(err,data) {
				if(err || !data){
					var info = {}
					for(var i in competitionCfg){
						info[i] = 0
					}
					console.log(competitionCfg,info)
					dbService.db.hmset("game:competition:inventory",info)
				}
			})
			//口红机配置及库存
			for(let i = 0;i < lipstickCfg.length;i++){
				dbService.db.exists("game:lipstick:"+i,function(err,data) {
					if(err || !data){
						dbService.db.hmset("game:lipstick:"+i,lipstickCfg[i])
					}
				})
			}
		})
	})
}